/*
* @Author: Marte
* @Date:   2018-11-02 11:01:52
* @Last Modified by:   Marte
* @Last Modified time: 2018-11-02 17:30:55
*/

function Section(){
    this.creatDepartment();
    this.addListener();


}
Section.template = ` <li><a href="#" class="option"><%= department %></a></li>
          `;
Section.item=`
    <tr>
        <td class="staff-id"><%= _id %></td>
        <td class="staff-name"><%= name %></td>
        <td class="staff-dep"><%= department %></td>
        <td class="staff-pro"><%= profession %></td>
        <td class="staff-entcom"><%= entercompanytime %></td>
        <td class="staff-salary"><%= salary %></td>

    </tr>

`;
// <td><a href="" class="update" data-toggle="modal" data-target="#UpdateStaffModal">修改</a> <a href="javascript:void(0);" class="del">删除</a></td>
$.extend(Section.prototype,{
    creatDepartment(){
        $(".section").css("background","#e6e6e6");
        const url = "/sections/department";
        $.post(url,(data)=>{
            console.log(data);
            var datas = data.res_body.data;
            if(data.res_code===1){
                var list =[];
                var obj ={};
                for(var i=0; i<datas.length;i++){
                    if(!obj[datas[i].department]){
                        list.push(datas[i]);
                        obj[datas[i].department]=true;
                    }
                }

                console.log(list)
            let html = "";
            list.forEach((curr)=>{
                    html += ejs.render(Section.template, curr);
                });
                $(".select_option").html(html);

            }

        })
    },
    addListener(){
        $(".select_option").on("click",this.selectHandler);
        $(".pagination").on("click", "a", $.proxy(this.loadDataHandler, this));
    },
    selectHandler(event){
        var $src = $(event.target);
        var data = $src.text();
       $(".show_option").val($src.text());

        const url = "/sections/answer?department="+data;
        $.getJSON(url, (data) => {
            if (data.res_code === 1) {
                console.log(data.res_body);

                let html = "";
                data.res_body.data.forEach((curr)=>{
                    html += ejs.render(Section.item, curr);
                });
                $(".table-staff tbody").html(html);
            }
        });
        // console.log(data);


    },
    loadDataHandler(event) {
        const $src = $(event.target);
        const page = Number($src.text());
        this.loadData(page);

        $src.parent("li").addClass("active").siblings("li").removeClass("active");
    },

    loadData(page) {

        page = page || 1;

        const url = "/sections/find_by_page?page=" + page;

        $.getJSON(url, (data) => {
            if (data.res_code === 1) {
                console.log(data.res_body);
                let html = "";
                data.res_body.list.forEach((curr)=>{
                    html += ejs.render(Section.item, curr);
                });
                $(".table-staff tbody").html(html);
            }
        });
    },
});
new Section();